package com.example.about_netty.util;

import org.junit.Test;

public class EasyUtils {

    public static boolean isPowerOfTwo(int val) {
        System.err.println(Integer.toBinaryString(val));//100
        System.err.println(Integer.toBinaryString(-val));//11111111111111111111111111111100
        return (val & -val) == val;
    }

    public static boolean isPowerOfTwo1(int val) {
        System.err.println(Integer.toBinaryString(val));//100
        System.err.println(Integer.toBinaryString(val - 1));//11
        return (val & (val - 1)) == 0;
    }

    //采用位运算 效率更快
    public static int powerOfTwoDistribute(int index, int val) {
        return index & (val - 1);
    }


    public static int modDistribute(int index, int val) {
        return index % val;
    }


    @Test
    public void 测试isPowerOfTwo() {
        boolean powerOfTwo = isPowerOfTwo(4);
        System.err.println(powerOfTwo);
    }

    @Test
    public void 测试isPowerOfTwo1() {
        boolean powerOfTwo1 = isPowerOfTwo1(4);
        System.err.println(powerOfTwo1);
    }

    @Test
    public void 测试powerOfTwoDistribute() {
        int num = 8 ;
        for (int i = 0; i < num*2; i++) {
            System.err.println(powerOfTwoDistribute(i, num));
        }

    }

    @Test
    public void 测试modDistribute() {
        int num = 6 ;
        for (int i = 0; i < 10; i++) {
            System.err.println(modDistribute(i, num));
        }

    }

}
